﻿package mortal.game.view.group
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GroupController extends Controller
    {
        private var _groupModel:GroupMoudle;
        private var _groupCache:GroupCache;
        private var _groupProxy:GroupProxy;

        public function GroupController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._groupCache = cache.group;
            this._groupProxy = GameProxy.group;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._groupModel == null)
            {
                this._groupModel = new GroupMoudle();
                this._groupModel.addEventListener(WindowEvent.SHOW, this.onGroupModelShow);
                this._groupModel.addEventListener(WindowEvent.CLOSE, this.onGroupModelClose);
            }
            return this._groupModel;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.AutoReject, this.autoReject);
            Dispatcher.addEventListener(EventName.GroupPanel_ViewInited, this.viewInitedHandler);
            Dispatcher.addEventListener(EventName.GroupLeave, this.leaveGroup);
            Dispatcher.addEventListener(EventName.GroupKickOut, this.kickOutPlayer);
            Dispatcher.addEventListener(EventName.GroupModifyCaptain, this.modifyCaptain);
            Dispatcher.addEventListener(EventName.GroupApplyOper, this.groupApplyOper);
            Dispatcher.addEventListener(EventName.GroupInviteOper, this.groupInviteOper);
            Dispatcher.addEventListener(EventName.GroupCreateGroup, this.createGroupHandler);
            Dispatcher.addEventListener(EventName.GroupDisbanGroup, this.disbanGroup);
            Dispatcher.addEventListener(EventName.GroupSetting, this.changeSetting);
            Dispatcher.addEventListener(EventName.GroupApplyReplyOper, this.groupApplyRejectOper);
            Dispatcher.addEventListener(EventName.GroupInviteReplyOper, this.groupInviteRejectOper);
            return;
        }// end function

        private function onGroupModelShow(event:Event) : void
        {
            if (!Cache.instance.group.isInGroup)
            {
                Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            }
            NetDispatcher.addCmdListener(ServerCommand.GroupPlayerInfoChange, this.updateTeamMate);
            NetDispatcher.addCmdListener(ServerCommand.GetNearTeam, this.updateNearTeam);
            NetDispatcher.addCmdListener(ServerCommand.GroupInvite, this.updateInviteList);
            NetDispatcher.addCmdListener(ServerCommand.GroupApply, this.updeteApplyList);
            NetDispatcher.addCmdListener(ServerCommand.GetNearPlayer, this.updateNearPlayer);
            NetDispatcher.addCmdListener(ServerCommand.GroupSettingUpdate, this.updateSetting);
            Dispatcher.addEventListener(EventName.GroupGetNearPlayer, this.updateNearPlayer);
            Dispatcher.addEventListener(EventName.GroupGetNearTeam, this.getNearTeam);
            Dispatcher.addEventListener(EventName.GroupTabIndexChange, this.changeTabIndex);
            Dispatcher.addEventListener(EventName.SceneAddGroupmate, this.updateTeamMate);
            Dispatcher.addEventListener(EventName.SceneRemoveGroupmate, this.updateTeamMate);
            return;
        }// end function

        private function onGroupModelClose(event:Event) : void
        {
            Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            NetDispatcher.removeCmdListener(ServerCommand.GroupPlayerInfoChange, this.updateTeamMate);
            NetDispatcher.removeCmdListener(ServerCommand.GetNearTeam, this.updateNearTeam);
            NetDispatcher.removeCmdListener(ServerCommand.GroupInvite, this.updateInviteList);
            NetDispatcher.removeCmdListener(ServerCommand.GroupApply, this.updeteApplyList);
            NetDispatcher.removeCmdListener(ServerCommand.GetNearPlayer, this.updateNearPlayer);
            Dispatcher.removeEventListener(EventName.GroupGetNearPlayer, this.updateNearPlayer);
            Dispatcher.removeEventListener(EventName.GroupGetNearTeam, this.getNearTeam);
            Dispatcher.removeEventListener(EventName.GroupTabIndexChange, this.changeTabIndex);
            Dispatcher.removeEventListener(EventName.SceneAddGroupmate, this.updateTeamMate);
            Dispatcher.removeEventListener(EventName.SceneRemoveGroupmate, this.updateTeamMate);
            return;
        }// end function

        private function updateTeamMate(param1:Object = null) : void
        {
            if (this._groupModel)
            {
                this._groupModel.updateTeamMate();
            }
            return;
        }// end function

        private function viewInitedHandler(event:DataEvent) : void
        {
            this._groupModel.showSkin();
            return;
        }// end function

        private function updateLevel(event:Event = null) : void
        {
            this._groupModel.updateTeamMate();
            return;
        }// end function

        private function disbanGroup(event:DataEvent = null) : void
        {
            var alarmText:String;
            var e:* = event;
            if (Cache.instance.group.isInCopyGroup)
            {
                var closeAlert:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    _groupProxy.disbandGroup();
                }
                return;
            }// end function
            ;
                if (CopyUtil.isInGroupCopy)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35306), MsgRollTipsType.msgRollTips3);
                }
                if (Cache.instance.copy.isInCopy && Cache.instance.copy.copyInfo.type == ECopyType._ECopyTypeTeam)
                {
                    alarmText = Language.getString(35307);
                    Alert.show(alarmText, null, Alert.OK | Alert.CANCEL, null, closeAlert);
                }
                else
                {
                    Alert.show(Language.getString(35308), null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
                }
            }
            else
            {
                this._groupProxy.disbandGroup();
            }
            return;
        }// end function

        private function createGroupHandler(event:DataEvent) : void
        {
            this._groupProxy.createGroup();
            return;
        }// end function

        private function updateNearPlayer(event:DataEvent = null) : void
        {
            this._groupModel.updateNearPlayer();
            return;
        }// end function

        private function getNearTeam(event:DataEvent) : void
        {
            var _loc_4:EntityInfo = null;
            var _loc_5:Boolean = false;
            var _loc_6:SEntityId = null;
            var _loc_2:* = new Array();
            var _loc_3:* = Cache.instance.entity.getAllEntityInfo(EEntityType._EEntityTypePlayer);
            for each (_loc_4 in _loc_3)
            {
                
                if (_loc_4.entityInfo.groupId.id != 0 && EntityUtil.isSelfProxy(_loc_4.entityInfo.entityId))
                {
                    for each (_loc_6 in _loc_2)
                    {
                        
                        if (_loc_6.id == _loc_4.entityInfo.groupId.id)
                        {
                            _loc_5 = true;
                            break;
                        }
                    }
                    if (!_loc_5)
                    {
                        _loc_2.push(_loc_4.entityInfo.groupId);
                    }
                }
            }
            this._groupProxy.getGroupInfos(_loc_2);
            return;
        }// end function

        private function updateNearTeam(param1:Object = null) : void
        {
            this._groupModel.updateNearTeam();
            return;
        }// end function

        private function updateInviteList(param1:Object = null) : void
        {
            this._groupModel.updateInviteList();
            return;
        }// end function

        private function kickOutPlayer(event:DataEvent) : void
        {
            if (CopyUtil.isInGroupCopy)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35309), MsgRollTipsType.msgRollTips3);
            }
            var _loc_2:* = event.data as SEntityId;
            this._groupProxy.kickOut(_loc_2);
            return;
        }// end function

        private function leaveGroup(event:DataEvent = null) : void
        {
            var data:GroupCopyLeaveOperData;
            var str:String;
            var alarmText:String;
            var copy:TCopy;
            var e:* = event;
            data = e.data as GroupCopyLeaveOperData;
            if (Cache.instance.group.isInGroup)
            {
                var closeAlert:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    _groupProxy.leftGroup(data);
                }
                return;
            }// end function
            ;
                if (data == null)
                {
                    this._groupProxy.leftGroup();
                }
                else if (Cache.instance.copy.isInCopy && Cache.instance.copy.copyInfo.type == ECopyType._ECopyTypeTeam)
                {
                    alarmText = Language.getString(35310);
                    Alert.okLabel = Language.getString(35311);
                    data;
                    Alert.show(alarmText, null, Alert.OK | Alert.CANCEL, null, closeAlert);
                }
                else if (data.isShowAler)
                {
                    if (data.type == GroupCopyLeaveOperData.RegisterArena)
                    {
                        str = Language.getStringByParam(35312, data.tcopy.name);
                    }
                    else if (data.tcopy)
                    {
                        if (Cache.instance.group.isCaptain)
                        {
                            str = Language.getStringByParam(30817, data.tcopy.name);
                        }
                        else
                        {
                            str = Language.getStringByParam(30817, data.tcopy.name);
                        }
                    }
                    else if (Cache.instance.group.isInCopyGroup)
                    {
                        copy = CopyConfig.instance.getCopyInfoByCode(Cache.instance.group.sgroup.copyCode);
                        str = Language.getStringByParam(35313, copy.name);
                    }
                    else
                    {
                        str = Language.getString(35314);
                    }
                    Alert.show(str, null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
                }
                else if (!data.isShowAler)
                {
                    this._groupProxy.leftGroup(data);
                }
            }
            else
            {
                this._groupProxy.leftGroup();
            }
            return;
        }// end function

        private function updeteApplyList(param1:Object = null) : void
        {
            if (this._groupModel)
            {
                this._groupModel.updateApplyList();
            }
            return;
        }// end function

        private function modifyCaptain(event:DataEvent = null) : void
        {
            this._groupProxy.modifyCaptain(event.data as SEntityId);
            return;
        }// end function

        private function changeSetting(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SGroupSetting;
            var _loc_3:* = Cache.instance.role.entityInfo.combat;
            if (_loc_2.combatLimit > _loc_3)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35315), MsgRollTipsType.msgRollTips3);
            }
            this._groupProxy.updateGroupSetting(_loc_2);
            return;
        }// end function

        private function updateSetting(param1:Object = null) : void
        {
            this._groupModel.updateSetting();
            return;
        }// end function

        private function changeTabIndex(event:DataEvent = null) : void
        {
            var _loc_2:* = event.data as int;
            this._groupModel.changeTabIndex(_loc_2);
            return;
        }// end function

        private function groupInviteOper(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            this._groupProxy.groupOper(_loc_2);
            return;
        }// end function

        private function groupApplyOper(event:DataEvent) : void
        {
            var _loc_4:GroupCopyLeaveOperData = null;
            var _loc_2:* = event.data as Array;
            var _loc_3:* = _loc_2[0];
            if (Cache.instance.group.isInGroup)
            {
                _loc_4 = new GroupCopyLeaveOperData(GroupCopyLeaveOperData.Apply);
                _loc_4.sgroupOper = _loc_3;
                Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave, _loc_4));
            }
            else
            {
                this._groupProxy.groupOper(_loc_2);
            }
            return;
        }// end function

        private function groupApplyRejectOper(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = event.data as Array;
            this._groupProxy.groupOper(_loc_2);
            if (_loc_2.length == 1)
            {
                while (_loc_3 < this._groupCache.applyList.length)
                {
                    
                    if ((this._groupCache.applyList[_loc_3] as SGroupOper).uid == (_loc_2[0] as SGroupOper).uid)
                    {
                        this._groupCache.applyList.splice(_loc_3, 1);
                    }
                    _loc_3++;
                }
            }
            else
            {
                this._groupCache.applyList = [];
            }
            this.updeteApplyList();
            return;
        }// end function

        private function groupInviteRejectOper(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = event.data as Array;
            this._groupProxy.groupOper(_loc_2);
            if (_loc_2.length == 1)
            {
                if ((_loc_2[0] as SGroupOper).copyCode)
                {
                    while (_loc_3 < this._groupCache.groupCopyInviteList.length)
                    {
                        
                        if (EntityUtil.equal(this._groupCache.groupCopyInviteList[_loc_3].groupOper.fromEntityId, (_loc_2[0] as SGroupOper).toEntityId))
                        {
                            this._groupCache.groupCopyInviteList.splice(_loc_3, 1);
                            break;
                        }
                        _loc_3++;
                    }
                }
                else
                {
                    _loc_3 = 0;
                    while (_loc_3 < this._groupCache.inviteList.length)
                    {
                        
                        if (EntityUtil.equal((this._groupCache.inviteList[_loc_3] as SGroupOper).fromEntityId, (_loc_2[0] as SGroupOper).toEntityId))
                        {
                            this._groupCache.inviteList.splice(_loc_3, 1);
                            break;
                        }
                        _loc_3++;
                    }
                }
            }
            else if ((_loc_2[0] as SGroupOper).copyCode)
            {
                this._groupCache.groupCopyInviteList.length = 0;
            }
            else
            {
                this._groupCache.inviteList.length = 0;
            }
            if (!GroupCopyInviteWin.instance.isHide)
            {
                GroupCopyInviteWin.instance.updateInviteList();
            }
            if (GameController.group.isViewShow)
            {
                this.updateInviteList();
            }
            return;
        }// end function

        private function autoReject(param1:Object) : void
        {
            var _loc_2:* = param1 as Array;
            this._groupProxy.groupOper(_loc_2);
            return;
        }// end function

    }
}
